package cn.trip.biz.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUseLinkUser;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.common.Md5Util;
import cn.itrip.common.Page;
import cn.itrip.common.RedisUtil;
import cn.itrip.common.TokenUtil;
import cn.trip.biz.service.FindUsertoUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api(value = "find|查询常用旅客的控制器")
@Controller
@RequestMapping("/api/userinfo")
public class FrequentVisitorEnquiriesController {
    @Resource(name = "FindUsertoUserService")
    private FindUsertoUserService findUsertoUserService;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private RedisUtil redisUtil;


    @ResponseBody
    @RequestMapping(value = "/queryuserlinkuser", method = RequestMethod.POST, headers = "token")
    @ApiOperation(value = "查询常用旅客列表信息", notes = "要输入账号,当前页" +
            "<p>证件类型(linkIdCardType)证件类型：(0-身份证，1-护照，2-学生证，3-军人证，4-驾驶证，5-旅行证)：</p>"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "usercode", value = "账号", required = true),
            @ApiImplicitParam(paramType = "query", name = "currentPage", value = "当前页")
    })
    public Dto findcommonlyUser(HttpServletRequest request, @RequestBody Map<String, String> linkUsemap) {
        Dto dto = null;
        String token = request.getHeader("token");

        String usercode = "";
        List<ItripUseLinkUser> passengersInfo = null;
        if (tokenUtil.isExist(token)) {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("user")) {
                        usercode = cookie.getValue();
                    }
                }
            }
            ItripUser users = findUsertoUserService.selectUserid(usercode);
            long Id = users.getId();
            passengersInfo = findUsertoUserService.findUsertosession(linkUsemap.get("linkUserName"), users);
            dto = new Dto();
            dto.setSuccess("true");
            dto.setErrorCode("0");
            dto.setMsg("列表显示成功");
            dto.setData(passengersInfo);
        } else {
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("token失效，请重新登录");

        }
        return dto;
    }

    @ResponseBody
    @RequestMapping(value = "/modifyuserlinkuser", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "修改常用联系人接口", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "修改常用联系人信息" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p> : 修改常用联系人失败 </p>" +
            "<p> : 不能提交空，请填写常用联系人信息</p>" +
            "<p> : token失效，请重登录 </p>")
    public Dto updateUserLinkUser(@RequestBody Map<String, String> linkusermap, HttpServletRequest request) {
        Dto dto = null;
        String token = request.getHeader("token");

        int userid = Integer.parseInt((token.substring(6, 8)));
        ItripUseLinkUser linkUser = new ItripUseLinkUser();
        linkUser.setUserId(userid);
        linkUser.setModifyDate(new Date());
        linkUser.setId(Long.parseLong(linkusermap.get("id")));
        linkUser.setLinkIdCard(linkusermap.get("linkIdCard"));
        linkUser.setLinkIdCardType(Integer.parseInt(linkusermap.get("linkIdCardType")));
        linkUser.setLinkPhone(linkusermap.get("linkPhone"));
        linkUser.setLinkUserName(linkusermap.get("linkUserName"));

        if (tokenUtil.isExist(token)) {
            if (null != linkusermap) {
                if (findUsertoUserService.setlinkUser(linkUser)) {
                    dto = new Dto();
                    dto.setSuccess("true");
                    dto.setErrorCode("0");
                    dto.setMsg("信息修改成功");
                } else {
                    dto = new Dto();
                    dto.setSuccess("false");
                    dto.setMsg("信息修改失败");
                }
            } else {
                dto = new Dto();
                dto.setSuccess("false");
                dto.setMsg("信息不能为空，请重新填写");
            }
        } else {
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("token失效，请重新登录");
        }
        return dto;
    }

    @ApiOperation(value = "新增常用联系人接口", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "新增常用联系人信息" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p> : 新增常用联系人失败 </p>" +
            "<p> : 不能提交空，请填写常用联系人信息</p>" +
            "<p> : token失效，请重登录 </p>")
    @RequestMapping(value = "/adduserlinkuser", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto<Object> addUserLinkUser(@RequestBody Map<String, String> linkusermap, HttpServletRequest request) {
        Dto dto = null;
        String token = request.getHeader("token");

        int userid = Integer.parseInt((token.substring(6, 8)));
        ItripUseLinkUser linkUser = new ItripUseLinkUser();
        linkUser.setUserId(userid);
        linkUser.setCreationDate(new Date());
        linkUser.setLinkIdCard(linkusermap.get("linkIdCard"));
        linkUser.setLinkIdCardType(Integer.parseInt(linkusermap.get("linkIdCardType")));
        linkUser.setLinkPhone(linkusermap.get("linkPhone"));
        linkUser.setLinkUserName(linkusermap.get("linkUserName"));
        if (tokenUtil.isExist(token)) {
            if (null != linkusermap) {
                if (findUsertoUserService.addlinkUser(linkUser)) {
                    dto = new Dto();
                    dto.setSuccess("true");
                    dto.setErrorCode("0");
                    dto.setMsg("信息增加成功");
                } else {
                    dto = new Dto();
                    dto.setSuccess("false");
                    dto.setMsg("信息增加失败");
                }
            } else {
                dto = new Dto();
                dto.setSuccess("false");
                dto.setMsg("信息不能为空，请重新填写");
            }
        } else {
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("token失效，请重新登录");
        }
        return dto;
    }

    @ApiOperation(value = "删除常用联系人接口", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "删除常用联系人信息" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p> : 所选的常用联系人中有与某条待支付的订单关联的项，无法删除 </p>" +
            "<p> : 删除常用联系人失败 </p>" +
            "<p> : 请选择要删除的常用联系人</p>" +
            "<p> : token失效，请重登录 </p>")
    @RequestMapping(value = "/deluserlinkuser", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto delUserLinkUser(@RequestParam(required = false) Long[] ids, HttpServletRequest request) {
        Dto dto = null;
        String token = request.getHeader("token");
        int userid = Integer.parseInt((token.substring(6, 8)));
        if (tokenUtil.isExist(token)) {
            if (ids.length > 0) {
                for (long id : ids) {
                    try {
                        dto = findUsertoUserService.delect(id, userid);
                    } catch (Exception e) {
                        e.printStackTrace();
                        dto.setSuccess("false");
                        dto.setMsg("删除失败");
                    }
                }
                return dto;
            } else {
                dto = new Dto();
                dto.setMsg("请选择要删除的常用联系人");
                dto.setSuccess("false");
            }
        } else {
            dto = new Dto();
            dto.setSuccess("false");
            dto.setMsg("token失效，请重新登录");
        }
        return dto;
    }
}
